package com.aat.dxfy.jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
/**
 * 读取配置文件
 * @author WuXin
 *
 */
public class Config {
	private Properties p = new Properties();

	/**
	 * read by a string file name<br>
	 * 直接从字符路径下的文件名读取
	 */
	public Config(String file) {
		InputStream inStream;
		try {
			inStream = new FileInputStream(file);
			p.load(inStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * read by IO input
	 * @param in
	 */
	public Config(InputStream in) {
		try {
			p.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * read by file
	 * @param file
	 */
	public Config(File file) {
		try {
			InputStream in = new FileInputStream(file);
			p.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * read by URI
	 * @param uri
	 */
	public Config(URI uri) {
		this(new File(uri));
	}

	/**
	 * read by URL
	 * @param url
	 */
	public Config(URL url) {
		this(url.getFile());
	}

	public int getInt(String key) {
		return Integer.parseInt(p.getProperty(key));
	}

	public String getString(String key) {
		return p.getProperty(key);
	}

	public double getDouble(String key) {
		return Double.parseDouble(p.getProperty(key));
	}

}
